﻿using Microsoft.Practices.Unity;
using Sasa.QualityTools.CodeAnalysis.Common;
using Sasa.QualityTools.CodeAnalysis.Core.Models;
using Sasa.QualityTools.CodeAnalysis.Core.Views;
using Sasa.QualityTools.CodeAnalysis.Core.VisualStudio;
using Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.ViewModels;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.ViewModels.Configurations;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Commands.Behaviors.Configuration
{
    public class ConfigureProjectBehavior : ConfigureBehaviorBase
    {
        private string configurationPath;


        protected override string ConfigurationPath
        {
            get
            {
                return configurationPath;
            }
        }


        public ConfigureProjectBehavior(IUnityContainer container)
            : base(container)
        {
        }


        public override void Behave(CommandExecutionContext context)
        {
            var ide = Container.Resolve<IIde>();

            var project = ide.Solution.CurrentProject;
            configurationPath = project.ConfigurationPath;
            
            var view = Container.Resolve<ICodeAnalysisConfigurationView>("project");

            var config = CodeAnalysisConfiguration.Load(ConfigurationPath);
            var msbuild = MSBuildProject.Load(project.ProjectPath);
            project.DependencyDirectories.ForEach(o => msbuild.DependencyDirectories.Add(o));

            var model = new CodeAnalysisConfigurationViewModel(config, msbuild);
            model.IsSupportedCodeAnalysis = project.IsSupportedCodeAnalysis;

            FillRuleSets(model);

            model.GenericConfiguration.BrowseRuleSetRequired += delegate
            {
                var path = BrowseRuleSet();
                if (path != null)
                {
                    if (model.GenericConfiguration.RuleSets.Count(o => o.Path == path) == 0)
                    {
                        model.GenericConfiguration.RuleSets.Add(new RuleSetViewModel(new RuleSet(path)));
                    }
                    model.GenericConfiguration.RuleSet = path;
                    var directory = project.Items.FirstOrDefault(o => o.Name == Core.Constants.CodeAnalysisConfigurationDirName && o.Kind == ProjectItemKind.Directory);
                    if (directory == null)
                    {
                        directory = project.AddFromDirectory(Path.GetDirectoryName(path));
                    }
                   directory.AddFromFile(path);
                }
            };
            model.Saving += delegate
            {
                ide.Checkout(project.ConfigurationPath);
                ide.Checkout(project.ProjectPath);
            };
            model.Saved += delegate
            {
                var directory = Path.GetDirectoryName(ConfigurationPath);
                project.AddFromDirectory(directory).AddFromFile(ConfigurationPath);
                project.Save();
            };
            model.BuildConfiguration.ShowProjectSiteRequired += delegate
            {
                ide.Navigate(CommonConstants.ProjectSiteUrl);
            };
            model.CloseRequired += delegate
            {
                view.Close();
            };

            view.DataContext = model;
            view.ShowModal();
        }
    }
}
